package ge.telasi.downloads;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author dimitri
 */
public class DownloadServlet extends HttpServlet {
    private static final long serialVersionUID = -643355524821277927L;

    static final String BASE = "/home/dimitri/Downloads/";
    static final String PROPERTIES = BASE + "download.properties";

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        OutputStream out = response.getOutputStream();
        try {
            // get download key
            String platform = request.getParameter("platform");
            String id = request.getParameter("id");
            String key = platform + "." + id;

            // get properties
            Properties prop = new Properties();
            prop.load(new FileInputStream(new File(PROPERTIES)));
            String path = prop.getProperty(key);
            File f = new File(BASE + path);

            // set headers
            String fileName = f.getName();
            response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
            String mimeType;
            if (fileName.endsWith("exe")) {
                mimeType = "application/exe";
            } else if (fileName.endsWith("sh")) {
                mimeType = "application/x-sh";
            } else if (fileName.endsWith("dmg")) {
                mimeType = "application/octet-stream";
            } else {
                mimeType = "application/octet-stream";
            }
            response.setContentType(mimeType);

            // write output
            InputStream in = new FileInputStream(f);
            byte[] buffer = new byte[2048];
            int length;
            while ((length = in.read(buffer)) != -1) {
                out.write(buffer, 0, length);
            }
            out.flush();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
